/*
 * Translated default messages for the jQuery validation plugin.
 * Locale: ZH (Chinese, 中文 (Zhōngwén), 汉语, 漢語)
 */
(function ($) {
    $.extend($.validator.messages, {
        required: "必填信息",
        remote: "请修正该信息",
        email: "请输入正确格式的电子邮件",
        url: "请输入合法的网址",
        date: "请输入合法的日期",
        dateISO: "请输入合法的日期 (ISO).",
        number: "请输入合法的数字",
        digits: "只能输入整数",
        creditcard: "请输入合法的信用卡号",
        equalTo: "请再次输入相同的值",
        accept: "请输入拥有合法后缀名的字符串",
        maxlength: $.validator.format("请输入一个长度最多是 {0} 的字符串"),
        minlength: $.validator.format("请输入一个长度最少是 {0} 的字符串"),
        rangelength: $.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
        range: $.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
        max: $.validator.format("请输入一个最大为 {0} 的值"),
        min: $.validator.format("请输入一个最小为 {0} 的值")
    });
}(jQuery));

jQuery.validator.addMethod("dictType", function (value, element) {
    var type = $("#type").val();
    if (type == null || type.trim() == '') {
        return false;
    }
    return true;
}, "请首先输入类型");

//自定义方法，完成手机号码的验证
//name:自定义方法的名称，method：函数体, message:错误消息
$.validator.addMethod("phone", function (value, element, param) {
    // 方法中又有三个参数:value:被验证的值， element:当前验证的dom对象，param:参数(多个即是数组)
    // alert(value + "," + $(element).val() + "," + param[0] + "," + param[1]);
    if (value != "") {
        return new RegExp(/^1[123456789]\d{9}$/).test(value);
    } else {
        return true;
    }

}, "手机号码不正确");

jQuery.validator.addMethod("ip", function (value, element) {
    return this.optional(element) || (/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.test(value) && (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256));
}, "请输入合法的IP地址");

jQuery.validator.addMethod("abc", function (value, element) {
    return this.optional(element) || /^[a-zA-Z0-9_]*$/.test(value);
}, "请输入字母数字或下划线");

jQuery.validator.addMethod("username", function (value, element) {
    return this.optional(element) || /^[a-zA-Z0-9][a-zA-Z0-9_]{2,19}$/.test(value);
}, "3-20位字母或数字开头，允许字母数字下划线");

//密码校验
jQuery.validator.addMethod("password", function (value, element) {
    var a = this.optional(element) || /^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$/.test(value)
        || /^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$/.test(value) || /^(?:\d+|[a-zA-Z]+|[!@#$%^&*]+)$/.test(value);
    var b = value.length >= 6 && value.length < 20;
    return a && b;
}, "密码格式错误，6-20位允许字母数字特殊字符");

jQuery.validator.addMethod("noEqualTo", function (value, element, param) {
    return value != $(param).val();
}, "请再次输入不同的值");

//真实姓名验证
jQuery.validator.addMethod("realName", function (value, element) {
    return this.optional(element) || /^[\u4e00-\u9fa5]{2,30}$/.test(value);
}, "姓名只能为2-30个汉字");

// 字符验证
jQuery.validator.addMethod("userName", function (value, element) {
    return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
}, "登录名只能包括中文字、英文字母、数字和下划线");

// 手机号码验证
jQuery.validator.addMethod("mobile", function (value, element) {
    var length = value.length;
    return this.optional(element) || (length == 11 && /^(((1[0-9][0-9]{1}))+\d{8})$/.test(value));
}, "请正确填写您的手机号码");

// 电话号码验证
jQuery.validator.addMethod("simplePhone", function (value, element) {
    var tel = /^(\d{3,4}-?)?\d{7,9}$/g;
    return this.optional(element) || (tel.test(value));
}, "请正确填写您的电话号码");

// 电话号码验证     
/*jQuery.validator.addMethod("phone", function(value, element) {     
 var tel = /(^0[1-9]{1}\d{9,10}$)|(^1[3,5,8]\d{9}$)/g;
 return this.optional(element) || (tel.test(value));
 }, "格式为:固话为区号(3-4位)号码(7-9位),手机为:13,15,18号段");	*/

// 邮政编码验证
jQuery.validator.addMethod("zipCode", function (value, element) {
    var tel = /^[0-9]{6}$/;
    return this.optional(element) || (tel.test(value));
}, "请正确填写您的邮政编码");

//QQ号码验证
jQuery.validator.addMethod("qq", function (value, element) {
    var tel = /^[1-9][0-9]{4,}$/;
    return this.optional(element) || (tel.test(value));
}, "请正确填写您的QQ号码");

//校验身份证好
jQuery.validator.addMethod("card", function (value, element) {
    return this.optional(element) || checkIdcard(value);
}, "请输入正确的身份证号码(15-18位)");

//验证身份证函数
function checkIdcard(idcard) {
    idcard = idcard.toString();
    //var Errors=new Array("验证通过!","身份证号码位数不对!","身份证号码出生日期超出范围或含有非法字符!","身份证号码校验错误!","身份证地区非法!");
    var Errors = [true, false, false, false, false];
    var area = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    };
    var idcard, Y, JYM;
    var S, M;
    var idcard_array = [];
    idcard_array = idcard.split("");
    //地区检验
    if (area[parseInt(idcard.substr(0, 2))] == null) return Errors[4];
    //身份号码位数及格式检验
    switch (idcard.length) {
        case 15:
            if ((parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 || ((parseInt(idcard.substr(6, 2)) + 1900) % 100 == 0 && (parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 )) {
                ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性
            } else {
                ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性
            }
            if (ereg.test(idcard)) return Errors[0];
            else return Errors[2];
            break;
        case 18:
            //18 位身份号码检测
            //出生日期的合法性检查
            //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
            //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
            if (parseInt(idcard.substr(6, 4)) % 4 == 0 || (parseInt(idcard.substr(6, 4)) % 100 == 0 && parseInt(idcard.substr(6, 4)) % 4 == 0 )) {
                ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式
            } else {
                ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式
            }
            if (ereg.test(idcard)) {//测试出生日期的合法性
                //计算校验位
                S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
                    + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
                    + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
                    + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
                    + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
                    + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
                    + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
                    + parseInt(idcard_array[7]) * 1
                    + parseInt(idcard_array[8]) * 6
                    + parseInt(idcard_array[9]) * 3;
                Y = S % 11;
                M = "F";
                JYM = "10X98765432";
                M = JYM.substr(Y, 1);//判断校验位
                if (M == idcard_array[17]) return Errors[0]; //检测ID的校验位
                else return Errors[3];
            }
            else return Errors[2];
            break;
        default:
            return Errors[1];
            break;
    }
}

//校验englishName是否是英文及数字集合
jQuery.validator.addMethod("englishCode", function (value, element, param) {
    var b = /^[0-9a-zA-Z]*$/g;
    if (b.test(value)) {
        return true;
    } else {
        //测试不通过，则输出提示
        return false;
    }
}, "请输入数字或字母");
//校验maxNo的大小
jQuery.validator.addMethod("validateMaxNo", function (value, element, param) {
    var minNo = $("#minNo").val();
    var standardNo = $("#standardNo").val();
    var flag = true;
    if (minNo == "") {
        minNo = "0";
    }
    if (minNo == "" && standardNo == "") {
        flag = true;
    } else if (minNo != "" && (parseInt(value) > parseInt(standardNo))) {
        flag = true;
    } else if (standardNo == "" && (parseInt(value) > parseInt(minNo))) {
        flag = true;
    } else if ((parseInt(value) > parseInt(minNo)) && (parseInt(value) > parseInt(standardNo))) {
        flag = true;

    } else {
        flag = false;
    }
    return flag;
}, "输入的值要比最小容量或标准容量大");
//校验minNo的大小
jQuery.validator.addMethod("validateMinNo", function (value, element, param) {
    var maxNo = $("#maxNo").val();
    var standardNo = $("#standardNo").val();
    var flag = true;
    if ((parseInt(maxNo) - parseInt(value)) < 0 || (parseInt(standardNo) - parseInt(value)) < 0) {
        return false;
    }
    else {
        return true;
    }
}, "输入的值要比最大容量或标准容量小");
//校验standarNo的大小
jQuery.validator.addMethod("validateStandardNo", function (value, element, param) {
    var maxNo = $("#maxNo").val();
    var minNo = $("#minNo").val();
    if (minNo == "") {
        minNo = "0";
    }
    var flag = true;
    if (maxNo == "" || minNo == "" || (parseInt(maxNo) - parseInt(value)) > 0) {
        if (minNo == "" || (parseInt(minNo) - parseInt(value)) < 0) {
        } else {
            flag = false;
        }
        return flag;
    }
    else {
        flag = false;
    }
    return flag;
}, "输入的值要小于最大容量，大于最小容量");

//校验parentSubjectCourseId的大小
jQuery.validator.addMethod("validateParentSelect", function (value, element, param) {
    var id = "${subjectCourseInfo.id}";
    if (id != null && id != '') {
        //alert($(this).val());
        if (id == $("#parentSubjectCourseId").val()) {
            return false;
        }
    }
    return true;
}, "不能选择自己为上级学科");
//校验课程介绍的长度
jQuery.validator.addMethod("validateDescription", function (value, element, param) {
    var description = $("#description").val();
    var length = description.length;
    if (length > 254) {
        return false;
    }
    return true;
}, "课程介绍长度在1到255个字符之间");

jQuery.validator.addMethod("point", function (v, element) {
    var r = true;
    if (v.indexOf(".") > 0) {
        var a = v.split(".");
        var c = a[0];
        var d = a[1];
        //alert(c + "---" + d);
        if (c.length > 3) {
            r = false;
        }
        if (d != "5") {
            if (d != "0") {
                r = false;
            }
        }
    } else {
        if (v.length > 3) {
            r = false;
        }
    }
    if (v.indexOf("-") >= 0) {
        r = false;
    }
    if (v.indexOf("0") == 0 && v.length != 1) {//不能以0开头
        if (v != "0.5") {
            r = false;
        }
    }
    return r;
}, "分数格式错误，只允许录入三位整数或小数");